package com.sduept.nwld.dataserver.manager.rota;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.rota.RotaPerson;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RotaPersonManager {
	
	@Autowired
	private SuperDAO sdao;
	/**
	 * 新增一条加班人员信息
	 * @param rp
	 * @return
	 */
	public RotaPerson createRotaPerson(RotaPerson rp){
		return sdao.create(rp, RotaPerson.class);
	}
	
	/**
	 * 更新加班人员信息
	 * @param rp
	 * @return
	 */
	public RotaPerson updateRotaPerson(RotaPerson rp){
		return sdao.update(rp, RotaPerson.class);
	}
	
	/**
	 * 根据日期获取加班人员
	 * @param roteDate
	 * @return
	 */
	public List<RotaPerson> findRotaPersonByRoteDate(Date roteDate){
		return sdao.findByCondition("rotaDate = ?",new Object[]{roteDate}, RotaPerson.class);		
	}
	
	/**
	 * 获取某月所有周末的值班人员信息
	 * @param monthInfo
	 * @return
	 */
	
	public List<RotaPerson> findRotaPersonByMonth(int year,int month){	
		try {
			Date startDate=DateUtil.getFirstDayOfMonth(year,month);
			Date endDate=DateUtil.getLastDayOfMonth(year,month);
			//TODO 临时解决方案 
			startDate.setHours(0);
			startDate.setMinutes(0);
			startDate.setSeconds(0);
			endDate.setHours(0);
			endDate.setMinutes(0);
			endDate.setSeconds(0);
			//and remark like '%周%'
			return sdao.findByCondition("rotaDate between ? and ? ",new Object[]{startDate,endDate}, RotaPerson.class);		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取某周末加班人员信息
	 * @param saturdayDate
	 * @param sundayDate
	 * @return
	 */
	public List<RotaPerson> findWeekRotePersonInDate(Date saturdayDate,Date sundayDate){		
		return sdao.findByConditionIN("rotaDate", new Object[]{saturdayDate,sundayDate}, RotaPerson.class);
	}
	
	/**
	 * 获取起止时间段内所有周末值班信息
	 * @param start
	 * @param end
	 * @return
	 */
	public List<RotaPerson> findAllWeekRotaPersonsBetweenStartDateAndEndDate(Date start,Date end){
		// and remark like '%周%'
		return sdao.findByCondition("rotaDate between ? and ?",new Object[]{start,end}, RotaPerson.class);		
	}
	
	/**
	 * 返回所有值班人员信息
	 * @return
	 */
	public List<RotaPerson> findAllRotaPerson(){
		return sdao.findAll(RotaPerson.class);
	}
	
	/**
	 * 返回某年所有值班人员信息
	 * @param year
	 * @return
	 */
	// and remark like '%周%'
	public List<RotaPerson> findAllRotaPersonByYear(int year){
		return sdao.findByCondition("year=?", new Object[]{year}, RotaPerson.class);
	}
	
	
	/**
	 * 返回所有周末值班人员信息
	 * @return
	 */
	public List<RotaPerson> findAllWeekRotaPerson(){
		return sdao.findByComplexCondition("From RotaPerson where remark like '%周%' order by rotaDate", RotaPerson.class);
	}
	
	/**
	 * 返回所有节假日值班人员信息
	 * @return
	 */
	public List<RotaPerson> findAllHolidayRotaPerson(){
		return sdao.findByConditionIN("remark", new Object[]{"元旦","春节","清明节","端午节","劳动节","国庆节","中秋节","国庆中秋节"}, null, "order by rotaDate", RotaPerson.class);
	}
	/**
	 * 删除所有值班人员信息
	 */
	public void delAllRotaPerson(){
		sdao.deleteAll(RotaPerson.class);
	}
	
	/**
	 * 根据id删除某条员工值班记录
	 * @param rpId
	 */
	public void delRotaPerson(String rpId){
		sdao.delete(rpId, RotaPerson.class);
	}
	

}
